package com.crwal.app.controller;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.crwal.app.bean.CrwalUrl;
import com.crwal.app.bean.News;
import com.crwal.app.config.MyCrwalAsyncTask;
import com.crwal.app.utils.Constant;
import com.crwal.app.utils.ExcelUtils;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@EnableScheduling
public class ScheduleTask {

	@Autowired
	private MyCrwalAsyncTask crwalAsync;
	
	@Scheduled(cron = "0 25 12 * * ?")
	public void crwalingDailyTask(){
		log.debug("开始抓取页面增量数据任务!");
		try {

			List<Map<String, String>> list = CrwalUrl.getList();
			for(Map<String, String> map : list){
				String fileName = map.get("fileName");
				String companyName = map.get("company");
				String companyHref = map.get("companyHref");
				
				//获取已有的最大发布时间
				String maxDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				//获取excel中已有的数据
				List<News> existList = new ArrayList<News>();
				boolean flag = FileUtil.exist(new File(CrwalUrl.getTempLocDirectory()+fileName));
				if(flag) {
					existList = ExcelUtils.readExcel(CrwalUrl.getTempLocDirectory(), fileName);
					//按照发布时间降序排列
					Collections.sort(existList, new Comparator<News>() {
			            @Override
			            public int compare(News o1, News o2) {
			                if (o1.getPublishDate().compareTo(o2.getPublishDate()) < 0) { //变成 < 可以变成递减排序
			                    return 0;
			                } else {
			                    return -1;
			                }
			            }
			        });
					if(existList != null && existList.size() > 0) {
						maxDate = existList.get(0).getPublishDate();
					}
				}
				
				List<News> newsList = new ArrayList<News>();
				//每天抓取大于最大发布时间的条目信息
				if(Constant.CrwalFileName.CAGILLNATUREWORKS.getName().equals(fileName)) {
					crwalAsync.natureworksllc(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.CORBION.getName().equals(fileName)) {
					crwalAsync.corbion(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.TOTALCORBION.getName().equals(fileName)) {
					crwalAsync.totalcorbion(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.PTTMCC.getName().equals(fileName)) {
					crwalAsync.pttmcc(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.CJBIO.getName().equals(fileName)) {
					crwalAsync.cjbio(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.BASF.getName().equals(fileName)) {
					crwalAsync.basf(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.ARKEMA.getName().equals(fileName)) {
					crwalAsync.arkema(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.PLASTICKER.getName().equals(fileName)) {
					crwalAsync.plasticker(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.BIOPLASTICSNEWS.getName().equals(fileName)) {
					crwalAsync.bioplasticsnews(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.BIOPLASTICSMAGAZINE.getName().equals(fileName)) {
					crwalAsync.bioplasticsmagazine(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.PLASTICSNEWS.getName().equals(fileName)) {
					crwalAsync.plasticsnews(companyName, companyHref, fileName, maxDate, flag, existList);
				} else if(Constant.CrwalFileName.BIOBASED.getName().equals(fileName)) {
					crwalAsync.biobased(companyName, companyHref, fileName, maxDate, flag, existList);
				}
				
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.debug("抓取页面增量数据任务出现异常!", e);
		}
		
	}
	
	
	
	

}
